import Mock from "mockjs";
// import { config } from "npm";

// get请求从config.url获取参数，post从config.body中获取参数
function param2Obj(url) {
  const search = url.split("?")[1];
  if (!search) {
    return {};
  }
  return JSON.parse(
    '{"' +
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"') +
      '"}'
  );
}

let List = [
  {
    id: "21907d13-792d-4d24-9578-238699a58d90",
    name: "赵超",
    addr: "海南省 三沙市 西沙群岛",
    age: 32,
    birth: "2002-04-25",
    sex: 0,
  },
  {
    id: "a26a8839-9f86-46d2-9902-9f3261dc27d3",
    name: "孙芳",
    addr: "浙江省 衢州市 江山市",
    age: 56,
    birth: "1993-07-12",
    sex: 1,
  },
  {
    id: "5d829c69-9d30-49d4-9633-7d7c9d98928d",
    name: "周勇",
    addr: "甘肃省 张掖市 肃南裕固族自治县",
    age: 21,
    birth: "2008-03-18",
    sex: 0,
  },
  {
    id: "92c69903-29d6-467a-9a78-78d2a0973d86",
    name: "吴丽",
    addr: "新疆维吾尔自治区 克拉玛依市 独山子区",
    age: 45,
    birth: "1984-09-09",
    sex: 1,
  },
  {
    id: "1229d84d-7822-4933-8957-89d9d99788d9",
    name: "郑刚",
    addr: "江苏省 泰州市 兴化市",
    age: 27,
    birth: "2002-01-23",
    sex: 0,
  },
  {
    id: "c9d8d8d9-99d7-4d32-99d8-99d9d9d9d9d9",
    name: "王芳",
    addr: "广东省 揭阳市 揭西县",
    age: 38,
    birth: "1991-06-15",
    sex: 1,
  },
  {
    id: "d8d9d9d9-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "李军",
    addr: "山东省 烟台市 海阳市",
    age: 52,
    birth: "1987-02-28",
    sex: 0,
  },
  {
    id: "9d9d9d9d-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "刘霞",
    addr: "四川省 广元市 青川县",
    age: 24,
    birth: "2005-08-07",
    sex: 1,
  },
  {
    id: "8d9d9d9d-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "陈强",
    addr: "云南省 普洱市 孟连傣族拉祜族佤族自治县",
    age: 41,
    birth: "1999-03-30",
    sex: 0,
  },
  {
    id: "7d9d9d9d-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "杨静",
    addr: "湖南省 娄底市 新化县",
    age: 35,
    birth: "1994-11-22",
    sex: 1,
  },
  {
    id: "6d9d9d9d-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "黄伟",
    addr: "贵州省 毕节市 威宁彝族回族苗族自治县",
    age: 29,
    birth: "2000-05-11",
    sex: 0,
  },
  {
    id: "5d9d9d9d-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "张敏",
    addr: "辽宁省 丹东市 东港市",
    age: 50,
    birth: "1989-07-19",
    sex: 1,
  },
  {
    id: "4d9d9d9d-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "徐峰",
    addr: "青海省 海东市 化隆回族自治县",
    age: 22,
    birth: "2007-12-03",
    sex: 0,
  },
  {
    id: "3d9d9d9d-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "朱红",
    addr: "宁夏回族自治区 固原市 隆德县",
    age: 47,
    birth: "1982-08-27",
    sex: 1,
  },
  {
    id: "2d9d9d9d-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "马丽",
    addr: "天津市 市辖区 宁河区",
    age: 33,
    birth: "1996-04-05",
    sex: 1,
  },
  {
    id: "1d9d9d9d-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "胡勇",
    addr: "西藏自治区 那曲市 班戈县",
    age: 58,
    birth: "1991-01-14",
    sex: 0,
  },
  {
    id: "0d9d9d9d-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "郭静",
    addr: "广西壮族自治区 贺州市 昭平县",
    age: 26,
    birth: "2003-09-21",
    sex: 1,
  },
  {
    id: "d9d9d9d9-99d8-4d32-99d8-99d9d9d9d9d8",
    name: "林强",
    addr: "黑龙江省 鸡西市 虎林市",
    age: 43,
    birth: "1996-10-17",
    sex: 0,
  },
  {
    id: "d9d9d9d9-99d8-4d32-99d8-99d9d9d9d9d7",
    name: "何芳",
    addr: "内蒙古自治区 呼伦贝尔市 新巴尔虎左旗",
    age: 30,
    birth: "2000-06-02",
    sex: 1,
  },
  {
    id: "d9d9d9d9-99d8-4d32-99d8-99d9d9d9d9d6",
    name: "高伟",
    addr: "陕西省 安康市 石泉县",
    age: 54,
    birth: "1985-03-24",
    sex: 0,
  },
  {
    id: "d9d9d9d9-99d8-4d32-99d8-99d9d9d9d9d5",
    name: "罗敏",
    addr: "福建省 南平市 浦城县",
    age: 23,
    birth: "2006-11-08",
    sex: 1,
  },
  {
    id: "d9d9d9d9-99d8-4d32-99d8-99d9d9d9d9d4",
    name: "梁军",
    addr: "江西省 上饶市 玉山县",
    age: 49,
    birth: "1990-07-13",
    sex: 0,
  },
  {
    id: "d9d9d9d9-99d8-4d32-99d8-99d9d9d9d9d3",
    name: "宋霞",
    addr: "湖北省 咸宁市 通山县",
    age: 36,
    birth: "1993-12-29",
    sex: 1,
  },
  {
    id: "d9d9d9d9-99d8-4d32-99d8-99d9d9d9d9d2",
    name: "唐强",
    addr: "安徽省 池州市 青阳县",
    age: 51,
    birth: "1988-02-20",
    sex: 0,
  },
  {
    id: "d9d9d9d9-99d8-4d32-99d8-99d9d9d9d9d1",
    name: "许静",
    addr: "重庆市 市辖区 大足区",
    age: 28,
    birth: "2001-05-06",
    sex: 1,
  },
  {
    id: "d9d9d9d9-99d8-4d32-99d8-99d9d9d9d9d0",
    name: "邓伟",
    addr: "北京市 市辖区 平谷区",
    age: 44,
    birth: "1995-08-18",
    sex: 0,
  },
  {
    id: "d9d9d9d8-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "曹敏",
    addr: "吉林省 四平市 双辽市",
    age: 31,
    birth: "1999-01-25",
    sex: 1,
  },
  {
    id: "d9d9d9d7-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "彭军",
    addr: "河南省 焦作市 孟州市",
    age: 57,
    birth: "1982-06-10",
    sex: 0,
  },
  {
    id: "d9d9d9d6-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "曾霞",
    addr: "山西省 吕梁市 交城县",
    age: 25,
    birth: "2004-09-22",
    sex: 1,
  },
  {
    id: "d9d9d9d5-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "萧伟",
    addr: "云南省 临沧市 云县",
    age: 42,
    birth: "1997-03-31",
    sex: 0,
  },
  {
    id: "d9d9d9d4-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "田静",
    addr: "贵州省 安顺市 普定县",
    age: 39,
    birth: "1990-11-15",
    sex: 1,
  },
  {
    id: "d9d9d9d3-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "董强",
    addr: "辽宁省 阜新市 彰武县",
    age: 53,
    birth: "1986-07-21",
    sex: 0,
  },
  {
    id: "d9d9d9d2-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "袁敏",
    addr: "青海省 海西蒙古族藏族自治州 德令哈市",
    age: 20,
    birth: "2009-02-07",
    sex: 1,
  },
  {
    id: "d9d9d9d1-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "于军",
    addr: "宁夏回族自治区 吴忠市 盐池县",
    age: 48,
    birth: "1981-08-28",
    sex: 0,
  },
  {
    id: "d9d9d9d0-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "余静",
    addr: "天津市 市辖区 静海区",
    age: 34,
    birth: "1995-04-16",
    sex: 1,
  },
  {
    id: "d9d9d8d9-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "叶伟",
    addr: "西藏自治区 日喀则市 定结县",
    age: 59,
    birth: "1980-01-15",
    sex: 0,
  },
  {
    id: "d9d9d7d9-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "蒋敏",
    addr: "广西壮族自治区 玉林市 陆川县",
    age: 27,
    birth: "2002-09-23",
    sex: 1,
  },
  {
    id: "d9d9d6d9-99d8-4d32-99d8-99d9d9d9d9d9",
    name: "杜军",
    addr: "黑龙江省 鹤岗市 萝北县",
    age: 46,
    birth: "1993-03-30",
    sex: 0,
  },
];
// const count = 200;
// //模拟200条用户数据
// for (let i = 0; i < count; i++) {
//   List.push(
//     Mock.mock({
//       id: Mock.Random.guid(),
//       name: Mock.Random.cname(),
//       addr: Mock.mock("@county(true)"),
//       "age|18-60": 1,
//       birth: Mock.Random.date(),
//       sex: Mock.Random.integer(0, 1),
//     })
//   );
// }

export default {
  /**
   * 获取列表
   * 要带参数 name, page, limt; name可以不填, page,limit有默认值。
   * @param name, page, limit
   * @return {{code: number, count: number, data: *[]}}
   */
  getUserList: (config) => {
    //limit默认是10，因为分页器默认也是一页10个
    const { name, page = 1, limit = 10 } = param2Obj(config.url);

    const mockList = List.filter((user) => {
      //如果name存在会，根据name筛选数据
      if (name && user.name.indexOf(name) === -1) return false;
      return true;
    });
    //分页
    const pageList = mockList.filter(
      (item, index) => index < limit * page && index >= limit * (page - 1)
    );
    return {
      code: 200,
      data: {
        list: pageList,
        count: mockList.length, //数据总条数需要返回
      },
    };
  },
  deleteUser: (config) => {
    const { id } = param2Obj(config.url);
    if (!id) {
      return {
        code: -999,
        message: "参数不正确",
      };
    } else {
      List = List.filter((u) => u.id !== id);
      return {
        code: 200,
        message: "删除成功",
      };
    }
  },
  /**
   * 增加用户
   * @param name, addr, age, birth, sex
   * @return {{code: number, data: {message: string}}}
   */
  addUser: (config) => {
    const { name, addr, age, birth, sex } = JSON.parse(config.body);
    List.unshift({
      id: Mock.Random.guid(),
      name: name,
      addr: addr,
      age: age,
      birth: birth,
      sex: sex,
    });
    return {
      code: 200,
      data: {
        message: "添加成功",
      },
    };
  },
  /**
   * 修改用户
   * @param id, name, addr, age, birth, sex
   * @return {{code: number, data: {message: string}}}
   */
  updateUser: (config) => {
    const { id, name, addr, age, birth, sex } = JSON.parse(config.body);
    const sex_num = parseInt(sex);
    List.some((u) => {
      if (u.id === id) {
        u.name = name;
        u.addr = addr;
        u.age = age;
        u.birth = birth;
        u.sex = sex_num;
        return true;
      }
    });
    return {
      code: 200,
      data: {
        message: "编辑成功",
      },
    };
  },
};
